/*

1、启动一个webServer
2、JS模块化
3、CSS模块化 （scss）
// 4、版本号控制
// 5、代码压缩
6、Mock数据



*/


// 暴露
// 引用
const connect = require('gulp-connect');
const webpack = require('webpack-stream'); //在nodejs环境中使用require
const sass = require('gulp-sass');
// 根据文件的内容产生一个哈希值（文件编码）
// const rev = require('gulp-rev');
// const revCollector = require('gulp-rev-collector');
const del = require('del');//删除包
// const cleanCSS = require('gulp-clean-css');
// const htmlmin = require('gulp-htmlmin');
const proxy = require('http-proxy-middleware');

const { 
  // 解构
  series,
  parallel ,
  src,
  dest,
  watch
} = require("gulp");

// ./表示当前目录

// ../表示父级目录

// /表示根目录
// 拷贝html
function copyHtml(){
  // return src('../src/*.html')
  // .pipe(dest('../dev'))
  // .pipe(connect.reload())
                   
  return src(['../src/*.html'])
  // .pipe(revCollector())
  // .pipe(htmlmin({ collapseWhitespace: true }))
  .pipe(dest('../dev'))
  .pipe(connect.reload())
}




// 拷贝assets
function copyAssets(){                 
  return src(['../src/assets/**/*'])
  .pipe(dest('../dev/assets'))
  .pipe(connect.reload())
}


// 拷贝libs
function copyLibs(){                 
  return src(['../src/libs/**/*'])
  .pipe(dest('../dev/libs'))
  .pipe(connect.reload())
}
// // 拷贝js          引进webpack-stream后就不需要这个了
// function copyJS(){
//   return src('../src/scripts*.js')
//   .pipe(dest('../scripts/dev'))
//   .pipe(connect.reload())
// }




// 启动一个本地的 web server
function webSever(){
  return connect.server({
      host:'localhost',
      root: '../dev',
      port: 8001,
      livereload: true,
      middleware(){
       return [
        proxy('/apii',{
          target:"https://m.jiuxian.com/m_v1/statics/getzx.htm?topicId=1165",
          changeOrigin:true,
          pathRewrite:{
            // 替换
            '^/apii':''
          }
        }),
      
        proxy('/guagua', {
          target: 'https://m.jiuxian.com/m_v1/dynamic/mob01ajax/151763?pageNum=2',
          changeOrigin: true,
          pathRewrite: {
            '^/guagua': ''
          }
        }),



        proxy('/home', {
          target: 'https://m.jiuxian.com/m_v1/dynamic/mob01ajax/151763?pageNum=2',
          changeOrigin: true,
          pathRewrite: {
            '^/home': ''
          }
        }),

        proxy('/fenlei', {
          target: 'https://m.jiuxian.com/m_v1/promote/qgajax.do?from=ad_03&pagenum=2&tabnum=1',
          changeOrigin: true,
          pathRewrite: {
            '^/fenlei': ''
          }
        }),




        
        proxy('/api',{
          target:"https://m.jiuxian.com/m_v1/promote/qgajax.do",
          changeOrigin:true,
          pathRewrite:{
            // 替换
            '^/api':''
          }
        }),

      ]
      }
    })
  
}


// JS模块    packJS 打包js
// ..........................packJS..............................................
function packJS(){
  return src('../src/scripts/app.js')
  .pipe(webpack(
    {
      mode:'development',
      // mode:'production',
    // 入口出口
      entry:'../src/scripts/app.js',
      output:{
      filename:'app.js'
  },
  
  // 设置node_modules不上传线上
  module: {
    rules: [
      {
        test: /\.js$/,  //正则   解析.js文件
        exclude: /(node_modules|bower_components)/,   //不包含里面的文件
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'] ,  //@babel/preset-env  预设  翻译各种各样的版本语法
            plugins: ['@babel/plugin-transform-runtime']
          }
        }
      },
      {
        test:/\.html$/,
        loader:'string-loader'
      }
    ]
  }
}
  ))
  .pipe(dest('../dev/scripts'))
 
  .pipe(connect.reload())
}
//``````````````````````````packloginJS```````````````````````````````````
function packloginJS(){
  return src('../src/scripts/login.js')
  .pipe(webpack(
    {
      mode:'development',
      // mode:'production',
    // 入口出口
      entry:'../src/scripts/login.js',
      output:{
      filename:'login.js'
  },
  
  // 设置node_modules不上传线上
  module: {
    rules: [
      {
        test: /\.js$/,  //正则   解析.js文件
        exclude: /(node_modules|bower_components)/,   //不包含里面的文件
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'] ,  //@babel/preset-env  预设  翻译各种各样的版本语法
            plugins: ['@babel/plugin-transform-runtime']
          }
        }
      },
      {
        test:/\.html$/,
        loader:'string-loader'
      }
    ]
  }
}
  ))
  .pipe(dest('../dev/scripts'))
 
  .pipe(connect.reload())
}
// ............................packclearSaleJS.................................
function packclearSaleJS(){
  return src('../src/scripts/clearSale.js')
  .pipe(webpack(
    {
      mode:'development',
      // mode:'production',
    // 入口出口
      entry:'../src/scripts/clearSale.js',
      output:{
      filename:'clearSale.js'
  },
  
  // 设置node_modules不上传线上
  module: {
    rules: [
      {
        test: /\.js$/,  //正则   解析.js文件
        exclude: /(node_modules|bower_components)/,   //不包含里面的文件
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'] ,  //@babel/preset-env  预设  翻译各种各样的版本语法
            plugins: ['@babel/plugin-transform-runtime']
          }
        }
      },
      {
        test:/\.html$/,
        loader:'string-loader'
      }
    ]
  }
}
  ))
  .pipe(dest('../dev/scripts'))
 
  .pipe(connect.reload())
}

// ...........................................................................

function packbWineJS(){
  return src('../src/scripts/bwine.js')
  .pipe(webpack(
    {
      mode:'development',
      // mode:'production',
    // 入口出口
      entry:'../src/scripts/bwine.js',
      output:{
      filename:'bwine.js'
  },
  
  // 设置node_modules不上传线上
  module: {
    rules: [
      {
        test: /\.js$/,  //正则   解析.js文件
        exclude: /(node_modules|bower_components)/,   //不包含里面的文件
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'] ,  //@babel/preset-env  预设  翻译各种各样的版本语法
            plugins: ['@babel/plugin-transform-runtime']
          }
        }
      },
      {
        test:/\.html$/,
        loader:'string-loader'
      }
    ]
  }
}
  ))
  .pipe(dest('../dev/scripts'))
 
  .pipe(connect.reload())
}


// 删除dev文件夹
function delDevFolder(){
   return del('../dev',{
     force:true
   })
}



// CSS模块化

function packCSS(){
  return src(['../src/styles/app.scss'])
  .pipe(sass().on('error', sass.logError))
  // .pipe(rev())
  // .pipe(cleanCSS({compatibility: 'ie8'}))
  .pipe(dest('../dev/styles'))
  // .pipe(rev.manifest())  //产生一个新的文件
  // .pipe(dest('../dev/rev/styles'))
  .pipe(connect.reload())
}

// 监听
function watcher(){
  watch("../src/*.html",series(copyHtml));
  // watch("../src/scripts/*.js",series(copyJS));
    watch("../src/scripts/**/*",packJS);
    watch("../src/styles/**/*.scss",packCSS);
    watch("../src/assets/**/*",copyAssets);
    watch("../src/libs/**/*",copyLibs);
    watch("../src/scripts/**/*",packloginJS);
    watch("../src/scripts/**/*",packclearSaleJS);
    watch("../src/scripts/**/*",packbWineJS);
    
}


  exports.default = series(delDevFolder,packJS,packloginJS,packbWineJS,packclearSaleJS,packCSS,parallel(copyAssets,copyLibs,copyHtml),parallel(webSever,watcher));
